package com.ruoyi.certificate.controller;

import com.ruoyi.certificate.domain.DidCertificate;
import com.ruoyi.certificate.service.IDidCertificateService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/certificate")
public class DidCertificateController {

    @Resource
    private IDidCertificateService certificateService;

    // 生成证书
    @PostMapping("/generate")
    public AjaxResult generate(@RequestParam Long userId, @RequestParam String publicKey) {
        DidCertificate cert = certificateService.generateCertificate(userId, publicKey);
        return AjaxResult.success(cert);
    }

    // 下载证书
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable Long id) {
        DidCertificate cert = certificateService.getCertificate(id);
        return AjaxResult.success(cert);
    }

    // 查询所有证书
    @GetMapping("/list")
    public AjaxResult list() {
        List<DidCertificate> list = certificateService.listCertificates();
        return AjaxResult.success(list);
    }

    // 注销证书
    @PostMapping("/revoke/{id}")
    public AjaxResult revoke(@PathVariable Long id) {
        boolean ok = certificateService.revokeCertificate(id);
        return ok ? AjaxResult.success("已注销") : AjaxResult.error("注销失败");
    }
}
